import { DB } from "../db"
import { ServerValues } from "../../../kds-base-define/src/ServerConfig"
import { Rpc } from "../rpc"
import { kdRpcMsg } from "kdweb-core/lib/rpc/protocols"
import { Log } from "../log"
import { RoomDefine } from "../../../kds-base-define/src/RoomDefine"
import { UserDefine } from "../../../kds-base-define/src/UserDefine"
import { kdutils } from "kdweb-core/lib/utils"
import { mongoDB } from "kdweb-core/lib/mongo/controller"
import { RoomCodeID } from "./room_id_code"

/** 2023/3/6 新增排行榜逻辑 */

let db = DB.get(ServerValues.dbBill)
let redis = DB.redis
let rankLongTableName = "t_long_rank"
let rankShortTableName = "t_short_rank"
let rankRedisTableName = "t_rank_bbwy"
async function setRankData(h: string, bill: any, roomID: number) {
    // await redis.hset(rankRedisTableName,roomID.toString(),bill,true)
    let t = -1;
    let long = await db.insert(rankLongTableName, bill)
    let short = await db.insert(rankShortTableName, bill)
    if (long && short) {
        t = 1;
    }
    return t > 0
}

async function getRankData(h: string, type: number, limit?: number) {
    let kv = {}
    kv["zhuang"] = { "$ne": type};
    let l: number = 50;
    if(limit){
        l = limit;
    }
    let guize = null;
    if(type == 0) {
        guize = { xScore: 1 , endDate: 1};
    }else{
        guize = { xScore: -1 , endDate: 1};
    }
    return await db.getOption(rankShortTableName, kv, {
        limit: l,
        // sort: { score: -1 , endDate: 1 }
        sort: guize
    })
}

async function getShortAllData(h: string, type: number) {
    let kv = {}
    kv["zhuang"] = { "$ne": type};
    let guize = null;
    if(type == 0) {
        guize = { xScore: 1 , endDate: 1};
    }else{
        guize = { xScore: -1 , endDate: 1};
    }
    return await db.getOption(rankShortTableName, kv, {
        sort: guize
    })
}

async function getLongAllData(h: string, type: number) {
    let kv = {}
    kv["zhuang"] = { "$ne": type};
    let guize = null;
    if(type == 0) {
        guize = { xScore: 1 , endDate: 1};
    }else{
        guize = { xScore: -1 , endDate: 1};
    }
    return await db.getOption(rankLongTableName, kv, {
        sort: guize
    })
}

async function getLongUserInfo(h: string, tag: number, userId: number) {
    return await db.getSingle(rankLongTableName,{ userID: userId , zhuang: {"$eq" : tag} })
}

async function getShortUserInfo(h: string, tag: number, userId: number) {
    return await db.getSingle(rankShortTableName,{ userID: userId , zhuang: {"$eq" : tag} })
}

async function getLongRankData(h: string, type: number, limit?: number) {
    let kv = {}
    kv["zhuang"] = { "$ne": type};
    let l: number = 50;
    if(limit){
        l = limit;
    }
    let guize = null;
    if(type == 0) {
        guize = { xScore: 1 , endDate: 1};
    }else{
        guize = { xScore: -1 , endDate: 1};
    }
    return await db.getOption(rankLongTableName, kv, {
        limit: l,
        // sort: { score: -1 , endDate: 1}
        sort: guize
    })
}

async function delShortAll(h: string) {
	return await db.delMany(rankShortTableName, {}) > 0
}

// room.tea
export let RpcRankTea = {
    set: setRankData,
    get: getRankData,
    getShortAll: getShortAllData,
    getLongAll:getLongAllData,
    getLong: getLongRankData,
    delShortAll: delShortAll,
    getLongUserInfo: getLongUserInfo,
    getShortUserInfo: getShortUserInfo
}
